<template>
    <section>
        <!--添加修改弹出框-->
        <el-dialog :title="title" :visible.sync="dialogForm">
            <el-form :model="addForm"  ref="addForm">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="零售价" prop="saleprice">
                    <el-input v-model="addForm.saleprice" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="成本价" prop="costprice">
                    <el-input v-model="addForm.costprice" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="产品图片" prop="resources">
                    <el-upload
                            v-model="addForm.resources"
                            class="upload-demo"
                            action="/api/file/upload"
                            :on-remove="handleRemove"
                            :on-success="handleSuccess"
                            :file-list="fileList"
                            list-type="picture">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                    </el-upload>
                </el-form-item>
                <el-form-item label="详情">
                    <!--<el-input type="textarea" v-model="addForm.intro"></el-input>-->
                    <quill-editor :options="editorOption" v-model="addForm.intro"></quill-editor>
                </el-form-item>
                <el-form-item label="预约须知">
                    <!--<el-input type="textarea" v-model="addForm.orderNotice"></el-input>-->
                    <quill-editor :options="editorOption" v-model="addForm.orderNotice"></quill-editor>
                </el-form-item>
            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="okForm">确 定</el-button>
            </div>
        </el-dialog>
        <!--高级查询工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true">
                <el-form-item>
                    <!--双向绑定的查询字段-->
                    <el-input v-model="keywords" placeholder="姓名"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-select v-model="states" clearable  placeholder="请选择状态">
                        <el-option label="上架" value="1"></el-option>
                        <el-option label="下架" value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <!--给按钮绑定事件请求方法-->
                    <el-button type="primary" v-on:click="getDepartments">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="warning" @click="handleAdd">新增</el-button>
                    <el-button type="success" @click="onsales" :disabled="this.sels.length===0">上架</el-button>
                    <el-button type="danger" @click="offsales" :disabled="this.sels.length===0">下架</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="products"  @selection-change="selsChange" style="width: 100%;" >
            <!--显示多选框-->
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column prop="name" label="店铺名称" >
            </el-table-column>
            <el-table-column prop="saleprice" label="商品售价" >
            </el-table-column>
            <el-table-column prop="offsaletime" label="下架时间" >
            </el-table-column>
            <el-table-column prop="onsaletime" label="上架时间" >
            </el-table-column>
            <el-table-column prop="state" label="状态">
                <template scope="scope">
                    <span style="color: green" v-if="scope.row.state==1">上架</span>
                    <span style="color: red" v-else-if="scope.row.state==0">下架</span>
                </template>
            </el-table-column>
            <el-table-column prop="costprice" label="成本价" >
            </el-table-column>
            <el-table-column prop="createtime" label="上传时间" >
            </el-table-column>
            <el-table-column prop="salecount" label="销售数量" >
            </el-table-column>
            <el-table-column label="操作">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>
    </section>
</template>

<script>
    import { quillEditor } from "vue-quill-editor"; //调用编辑器
    import 'quill/dist/quill.core.css';
    import 'quill/dist/quill.snow.css';
    import 'quill/dist/quill.bubble.css';
    import quillConfig from "../../common/quill-config"
    export default {
        //申明组件
        components: {
            quillEditor//:quillEditor
        },
        data() {
            return {
                quillEditor: quillConfig,
                /*定义校验规则*/
                rules: {
                    name: [
                        {required: true, message: '商铺名必填!!', trigger: 'blur'}
                    ]
                },
                //图片
                fileList: [],
                dialogForm:false,
                products:[],
                //当前页
                currentPage: 1,
                //当前页显示条数
                pageSize:10,
                //总条数
                total:0,
               //高级查询关键字
                keywords:'',
                states:'',
                sels:{},
                addForm:{
                    id:'',
                    name:'',
                    saleprice:'',
                    costprice:'',
                    resources:'',
                    intro:'',
                    orderNotice:''
                },
                title:''

            }
        },
        methods: {
            //批量删除
            batchRemove(){
                this.$confirm('确认删除吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http.post("/product/batchRemove",this.sels).then(res => {
                        let {success, message} = res.data;
                        if(success){
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            //重新刷新页面
                            this.getProductList();
                        }else{
                            this.$message.error(message);
                        }
                    });
                }).catch(() => {
                });
            },
            //上传成功
            handleSuccess(response, file, fileList){

                for(var i=0;i<fileList.length;i++){
                    if(i==0){
                        this.addForm.resources = fileList[i].response.resultObj;
                    }else{
                        this.addForm.resources = this.addForm.resources+","+ fileList[i].response.resultObj;
                    }
                }
            },
            //上传照片移除
            handleRemove(file, fileList){
                // var filePath =file.response.resultObj;
                // this.$http.delete("/common/fastDfs/?path="+filePath)
                //     .then(res=>{
                //         if(res.data.success){
                //             this.$message({
                //                 message: '删除成功!',
                //                 type: 'success'
                //             });
                //         }else{
                //             this.$message({
                //                 message: '删除失败!',
                //                 type: 'error'
                //             });
                //         }
                //     })
            },
            //高级查询
            getDepartments(){
                this.currentPage = 1
                this.getProductList()
            },
            //修改添加保存按钮
            okForm(){
                //创建服务详情对象
                var productDetail = {"orderNotice": this.addForm.orderNotice, "intro": this.addForm.intro};
                //给服务对象设置属性
                this.addForm.productDetail = productDetail;

                //在提交之前，form表单必须要先经过校验
                this.$refs["addForm"].validate((valid) => {
                    if (valid) {
                        //保存/修改 数据
                        this.$http.put("/product", this.addForm).then(res => {
                            let {success, message} = res.data;
                            if(success){
                                //关闭窗体
                                this.dialogForm = false;
                                //刷新界面
                                this.getProductList();
                            }else{
                                this.$message.error(message);
                            }
                        });
                    }
                });

            },
            //修改商品信息
            handleEdit(index,row){
                console.debug(row)
                this.title = "修改";
                //数据回显
                for(var k in this.addForm){
                    this.addForm[k]=row[k]
                }
                //如果服务详情有值，预约须知和详情就进行回显
                if(row.productDetail){
                    this.addForm.orderNotice = row.productDetail.orderNotice;
                    this.addForm.intro = row.productDetail.intro;
                }
                this.fileList = [];
                //附件要进行回显
                if(row.resources){
                    var images = row.resources.split(",");
                    for(var i=0;i<images.length;i++){
                        this.fileList.push({"url":this.$fastBaseUrl+images[i]});
                    }
                }
                this.dialogForm =true
            },
            //添加
            handleAdd(){
                this.title = "新增";
                //如果有校验，才清空校验
                if(this.$refs["addForm"]){
                    this.$refs["addForm"].resetFields();
                }
                //数据清空
                for(var k in this.addForm){
                    this.addForm[k]=''
                }
                this.fileList = [];
                this.dialogForm =true
            },
            //下架
            offsales(){
                this.$confirm('确认下架吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http.post("/product/offsale",this.sels).then(res => {
                        let {success, message} = res.data;
                        if(success){
                            this.$message({
                                type: 'success',
                                message: '下架成功!'
                            });
                            //重新刷新页面
                            this.getProductList();
                        }else{
                            this.$message.error(message);
                        }
                    });
                }).catch(() => {
                });
            },
            //上架
            onsales(){
                this.$confirm('确认上架吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http.post("/product/onsale",this.sels).then(res => {
                        let {success, message} = res.data;
                        if(success){
                            this.$message({
                                type: 'success',
                                message: '上架成功!'
                            });
                            //重新刷新页面
                            this.getProductList();
                        }else{
                            this.$message.error(message);
                        }
                    });
                 }).catch(() => {
                });
            },
            //页码发生改变的时候触发此函数
            handleCurrentChange(val){
                //重新修改页码
                this.currentPage = val;
                //重新加载列表
                this.getProductList();
            },
            selsChange(rows){
                this.sels = rows
            },
            //删除商品
            handleDel(index,row){
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(()=>{
                    this.$http.delete("/product/"+row.id).then(res=>{
                        //解构表达式
                        let {success,msg} = res.data
                        if(success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getProductList();
                        }
                    })
                }).catch(()=>{
                    this.$message("取消删除")
                })
            },
            getProductList(){
                //分页传递参数
                let param = {
                    "keyWords": this.keywords,
                    "states" : this.states,
                    "currentPage": this.currentPage,
                    "pageSize": this.pageSize
                };
                this.$http.post("/product",param).then(res=>{
                    console.debug(res)
                    let {data, totals} = res.data;
                    this.products = data;
                    this.total = totals;
                })
            }
        },
        mounted(){
            this.getProductList()
        }
    }
</script>
